/****************************************************
          File Name:binary_fifo.h
          Author: Abby Cin
          Mail: abbytsing@gmail.com
          Created Time: Tue 16 Sep 2014 08:50:38 PM CST
*****************************************************/
#ifndef BINARY_FIFO_
#define BINARY_FIFO_

#include<stdio.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<sys/types.h>
#include<errno.h>
#include<sys/shm.h>
#include<unistd.h>
#include<stdlib.h>

#define FIFO "./fifo"

/********************** START ***************************/
#define PERM (S_IRUSR|S_IWUSR|S_IWGRP|S_IRGRP)

#define SHM_KEY 0x0309

#ifndef BUF_SIZE
#define BUF_SIZE 1024
#endif

struct shmseg{
    int cnt;
    char buf[BUF_SIZE];
};

char buf[1];

int Reserved(void)
{
    int fd,res;
    if((fd=open(FIFO,O_RDWR,PERM))==-1)
        return -1;
    res=read(fd,buf,1);
	close(fd);
	return res;
}

int Release(void)
{
    int fd,res;
    if((fd=open(FIFO,O_RDWR,PERM))==-1)
        return -1;
    res=write(fd,"1",1);
	close(fd);
	return res;
}

int conReserved(void)
{
    int fd,res;
    if((fd=open(FIFO,O_RDWR|O_NONBLOCK,PERM))==-1)
        return -1;
    res=read(fd,buf,1);
	close(fd);
	return res;
}
/***************** END **********************/

void err(char *fun)
{
    perror(fun);
    exit(errno);
}

#endif
